<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>捕获组</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
    </style>
</head>
<body>
    
    <script type="text/javascript">
        /* 
        捕获组：  (xxx)
            1、将某段的内容作为一个整体进行匹配
            2、取出匹配的内容中的某个部分   exec/match 结果中输出捕获内容，不带g的情况

        非捕获组：仅让捕获组作为整体进行匹配，但是不将捕获结果输出 
            语法：(?:xxx)

        反向引用 \n  n表示第n个捕获组匹配的内容，非捕获不可以使用此项功能
        按名引用 \k<name>  引用前面 (?<name>xxx) 捕获的结果
        */

        var str = "hahahaaaahaa";

        console.log(str.match(/ha+/g)); // [ha, ha, haaaa, haa]
        console.log(str.match(/(ha)+/g)); // [hahaha, ha]

        var strr = "nethello worldwork";
        console.log(strr.match(/hello world/)); // [hello world, 3]
        console.log(strr.match(/hello world/)); // [hello world, hello, 3]

        var strrr = "nethellohello worldwork";
        console.log(strrr.match(/(hello)+ world/)); // [hellohello world, hello, 3]
        console.log(strrr.match(/(?:hello)+ world/)); // [hellohello world, 3]

        console.clear();
        // 我们要匹配一对儿以单引号或者双引号包裹的内容 '...' 或 "..."
        var str = `He said: "She's the one!".`;

        console.log(str.match(/['"][\s\S]+?['"]/));  // ["She']
        console.log(str.match(/(['"])([\s\S]+?)\1/));  // [""She's the one!""]
        console.log(str.match(/(?<startDot>['"])([\s\S]+?)\k<startDot>/));  // ["She's the one!"]

            

        var reg = /(a)(b)(c)(?<D>\w)(e)(?<F>f)(g)(h)(i)\k<F>\k<D>/;
        
        console.log("abc2efghif2".match(reg));
    </script>
</body>
</html>